﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using MyClever.Lib;

namespace MyCleverMissionEditor
{
    /// <summary>
    /// Part of "Composite Design Pattern"
    /// The 'Composite' class
    /// </summary>
    public class GuiTreeCompositeElement : GuiTreeDrawingElement
    {
        private List<GuiTreeDrawingElement> guiTreeElements;

        public override Size Size
        {
            get
            {
                Size elementSize = new Size();
                foreach (GuiTreeDrawingElement treeElementOn in this.guiTreeElements)
                {
                    elementSize += treeElementOn.Size;
                }
                return elementSize;
            }
        }

        public GuiTreeCompositeElement()
        {
            this.guiTreeElements = new List<GuiTreeDrawingElement>();
        }

        public override void Add(GuiTreeDrawingElement drawingElement)
        {
            this.guiTreeElements.Add(drawingElement);
        }

        public override void Clear()
        {
            this.guiTreeElements.Clear();
        }

        public override void Display(DoubleBufferedTabControl tabPage, Point position)
        {
            foreach (GuiTreeDrawingElement treeElementOn in this.guiTreeElements)
            {
                if (typeof(GuiTreeCompositeElement) == treeElementOn.GetType())
                {
                    treeElementOn.Display(tabPage, position);
                }
                else // GuiMissionPluginElement
                {
                    treeElementOn.Display(tabPage, position);
                    position.X += treeElementOn.Size.Width + 100;

                    //position.Y += treeElementOn.Size.Height + 20;
                }
            }
        }

        public override bool IsMouseOver(Point mousePosition, bool withBorder)
        {
            foreach (GuiTreeDrawingElement treeElementOn in this.guiTreeElements)
            {
                if (true == treeElementOn.IsMouseOver(mousePosition, withBorder))
                {
                    return true;
                }
            }
            return false;
        }

        public override RelativePluginPosition GetPlaceholderByMouse(Point mousePosition, out Rectangle placeholder)
        {
            placeholder = new Rectangle();

            foreach (GuiTreeDrawingElement treeElementOn in this.guiTreeElements)
            {
                if (true == treeElementOn.IsMouseOver(mousePosition, true))
                {
                    return treeElementOn.GetPlaceholderByMouse(mousePosition, out placeholder);
                }
            }
            return new RelativePluginPosition(DockOrientation.None, -1);
        }
    }
}
